<template>
  <basic-container>
    <avue-crud
      :option="option"
      :search.sync="search"
      :table-loading="loading"
      :data="data"
      :page.sync="page"
      :permission="permissionList"
      :before-open="beforeOpen"
      v-model="form"
      ref="crud"
      @row-del="rowDel"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @sort-change="sortChange"
      @on-load="onLoad"
    >
      <template slot="menuLeft">
        <!-- 设备终端商户端不可以新增，编辑，商户端只能解绑终端和设备之间的关系 ，代理商可以 -->
        <!-- <el-button type="primary" icon="el-icon-plus" size="small" v-if="selectMer.userMerchantType === 1||selectMer.userMerchantType === 2" @click.stop="handleSave">新 建</el-button> -->
        <!-- <el-button
          type="danger"
          size="small"
          icon="el-icon-delete"
          plain
          v-if="permission.terminal_delete"
          @click="handleDelete"
          >删 除
        </el-button> -->
      </template>
      <!-- <template slot="menuRight">
        <el-button
          type="success"
          size="small"
          plain
          icon="el-icon-upload2"
          v-if="permission.terminal_import"
          @click="handleImport"
          >导入
        </el-button>
        <el-button
          type="warning"
          size="small"
          plain
          icon="el-icon-download"
          v-if="permission.terminal_export"
          @click="handleExport"
          >导出
        </el-button>
      </template> -->
      
      <template slot-scope="scope" slot="menu">
        <el-button icon="el-icon-view" size="small" type="text" @click.stop="handleView(scope.row, scope.index)">查 看</el-button>
        <!-- <el-button icon="el-icon-edit" size="small" type="text" @click.stop="handleEdit(scope.row, scope.index)">修 改</el-button> -->
        <!-- <el-button icon="el-icon-key" size="small" type="text" v-if="permissionList.delBtn" @click.stop="rowDel(scope.row)">解 绑</el-button> -->
      </template>
      <template slot-scope="{row}" slot="rsSn">
        <div :class="row.rpmNo == 'R00001'||row.rptNo == '32' ? 'rsSnblue' : ' '" @click.stop="getqrCodeUrl(row)" >
          {{ row.rsSn }}
        </div>
      </template>
      <template slot-scope="{}" slot="rpiNoSearch">
        <select-page
          ref="searchRpiSelect"
          label="rpiName"
          value="rpiNo"
          :select.sync="search.rpiNo"
          :url="website.searchLink.merchantUrl"
          placeholder-text="商户"
          filter-key="searchParam"
          :params="{
            rpiGroup: selectMer.rpiNo
          }"
          @update:select="searchrpiNoChange"
          @clear:select="searchrpiNoClear"
        ></select-page>
      </template>
      <template slot-scope="{}" slot="storeNoSearch">
        <select-page
          ref="searchRpsSelect"
          label="rpsName"
          value="rpsNo"
          :select.sync="search.storeNo"
          :url="website.searchLink.rpsUrl"
          placeholder-text="门店"
          filter-key="searchParam"
          :params="searchParams"
          @update:select="searchrpsNoChange"
          @clear:select="searchrpsNoClear"
        ></select-page>
      </template>
      <!-- :params="{
            rpiNo: search.rpiNo
          }" -->
      <template slot-scope="{}" slot="rpstNoSearch">
        <select-page
          ref="searchRpstSelect"
          label="rpstName"
          value="rpstNo"
          :select.sync="search.rpstNo"
          :url="website.searchLink.rpstUrl"
          placeholder-text="收银员"
          filter-key="searchParam"
          :params="searchParams"
        ></select-page>
        <!-- :params="{
            rpiNo: search.rpiNo,
            rpsNo: search.storeNo,
          }" -->
      </template>
    </avue-crud>
    <!-- 导入 -->
    <el-dialog
      title="数据导入"
      append-to-body
      :visible.sync="excelBox"
      width="555px"
    >
      <avue-form
        :option="excelOption"
        v-model="excelForm"
        :upload-after="uploadAfter"
      >
        <template slot="excelTemplate">
          <el-button type="primary" @click="handleTemplate">
            点击下载<i class="el-icon-download el-icon--right"></i>
          </el-button>
        </template>
      </avue-form>
    </el-dialog>
    <!-- 新建 -->
    <el-dialog
      :title="terminalDialog.text"
      top="1%"
      append-to-body
      :close-on-click-modal="false"
      :visible.sync="terminalDialog.visible"
      v-if="terminalDialog.visible"
      :before-close="terminalDialogHandleClose"
      ref="terDialog"
    >
      <terminalDialog ref="terminalDialog" :terminalDialog="terminalDialog" :selectMer="selectMer" :rptNoList="rptNoList" :storeNoListZ="storeNoList" :terminalDForm="terminalDForm" />
      <span slot="footer" class="dialog-footer">
        <el-button
          @click="() => { terminalDialog.visible = false; this.$refs['terminalDialog'].initEmpty();}">取 消</el-button>
        <el-button type="primary" @click="terminalDialogSubmit" v-if="!terminalDialog.disabled">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 图片预览 -->
    <el-image-viewer
      style="z-index: 2048"
      v-if="payVoucherDialog"
      :on-close="()=>{ payVoucherDialog = false }"
      :url-list="[rqUrlImage]"
    ></el-image-viewer>
    <!-- 二级密码 -->
    <el-dialog
      title="二级密码"
      append-to-body
      width="440px"
      :close-on-click-modal="false"
      v-if="twoPwdShow"
      :visible.sync="twoPwdShow"
      :before-close="handleCloseTwoPwd"
    >
      <el-form :model="towPwdForm" ref="towPwdForm" label-width="0" class="demo-ruleForm">
        <el-form-item
          label=""
          prop="towPwd"
          :rules="[
            { required: true,message: '二级密码不能为空', trigger: ['change','blur']},
            { pattern: /^[0-9]{6}$/, message: '二级密码由6位数字组成', trigger: ['change','blur']}
          ]"
          >
          <el-input show-password v-model.number="towPwdForm.towPwd" autocomplete="off" placeholder="输入二级密码"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="twoPwdSubmit()">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 检测是否选择商户 -->
    <selectMer :dialogVisible="tipsShow" />
  </basic-container>
</template>

<script>
import { getModelList,getList,getDetail,addTerminal,editTerminal,remove,terRemove,getRptNo,removeSM,removePOS } from "@/api/rb-merchant/terminal/terminal";
// getrpiNo,  ,getstoreNo,getrpstNo
import {mapGetters} from "vuex";
import {getToken} from "@/util/auth";
import terminalDialog from "./terminalDialog";
import { userHasInfo } from "@/api/rb-merchant/forgetPassword/forgetPassword";
import md5 from 'js-md5';
// import { getMerchantList } from "@/api/rb-merchant/posinfo/pos_info";
import selectPage from "@/components/select-page/select-page";

export default {
  components: {
    terminalDialog,
    selectPage
  },
  data() {
    return {
      searchParams:{},
      rpiGroup:"",
      tipsShow:false,
      payVoucherDialog: false,
      rqUrlImage: [],
      form: {},
      query: {},
      search: {},
      loading: true,
      excelBox: false,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      selectionList: [],
      option:{
        calcHeight: 30,
        tip: false,
        border: true,
        index: true,
        selection: true,
        searchMenuSpan: 6,
        columnBtn: false,
        addBtn: false,//取消表格 新建/添加
        editBtn: false,
        delBtn: false,
        viewBtn: false,
        labelWidth: 100,
        viewTitle: '修 改',
        // menuWidth:200,
        column: [
          {
            label: "终端简称",
            prop: "rtName",
            type: "input",
            //sortable: true,
            display: false,
            overHidden: true,
            width:140
          },
          {
            label: "类型",
            prop: "rptName",
            type: "input",
            //sortable: true,
            display: false,
            overHidden: true,
          },
          {
            label: "类型",
            prop: "rptNo",
            type: "select",
            //sortable: true,
            search: true,
            searchOrder: 1,
            hide: true,
            display: false,
            rules: [
              {
                required: true,
                message: "请输入类型",
                trigger: "blur",
              },
            ],
            props: {
              label: 'rptName',
              value: 'rptNo',
              desc:'rptNo'
            },
            filterable: true,
            dicData: [],
            change: (value) => {
              if (value) {
                // 获取型号
                // this.search.rpsNo = '';
                // this.rpsNoList = [];
                this.search.rpmNo = '';
                this.rpmNoList = [];
                if (value.value) {
                  this.apiModelListList(value.value);//型号
                }
              }
            },
            overHidden: true,
          },
          {
            label: "型号",
            prop: "rpmName",
            type: "input",
            //sortable: true,
            display: false,
            overHidden: true,
          },
          {
            label: "型号",
            prop: "rpmNo",
            type: "select",
            //sortable: true,
            search: true,
            searchOrder: 3,
            hide: true,
            display: false,
            rules: [
              {
                required: true,
                message: "请输入型号",
                trigger: "blur",
              },
            ],
            props: {
              label: 'rpmName',
              value: 'rpmNo',
              desc:'rpmNo'
            },
            dicUrl: `/api/rb-merchant/merchant/terminal/terminal/modelList?current=-1&size=-1`,
            dicFormatter:(data)=>{
              return data.data.records
            },
            // dicData: [],
            filterable: true,
            overHidden: true,
          },
          {
            label: "商户编号",
            searchLabel: "商户",
            prop: "rpiNo",
            type: "select",
            search: false,
            display:false,
            hide: false,
            searchOrder: 4,
            minWidth: 180,
            overHidden: true,
          },
          {
            label: "商户",
            prop: "rpiName",
            type: "select",
            search: false,
            display:false,
            hide: false,
            searchOrder: 4,
            minWidth: 180,
            overHidden: true,
          },
          {
            label: "门店",
            prop: "storeName",
            type: "input",
            searchOrder: 3,
            display: false,
            overHidden: true,
            width: 180,
          },
          {
            label: "门店",
            prop: "storeNo",
            type: "select",
            search: false,
            display: false,
            searchOrder: 5,
            hide: true,
            overHidden: true,
          },
          {
            label: "收银员",
            prop: "rpstName",
            type: "input",
            //sortable: true,
            display: false,
            overHidden: true,
          },
          {
            label: "收银员",
            prop: "rpstNo",
            type: "select",
            search: false,
            display: false,
            hide: true,
            searchOrder: 6,
            searchLabelWidth: 94,
            overHidden: true,
          },
          {
            label: "终端编号",
            prop: "rsSn",
            type: "input",
            search: true,
            rules: [
              {
                required: true,
                message: "请输入终端编号",
                trigger: "blur",
              }
            ],
            overHidden: true,
          },
          {
            label: "绑定时间",
            prop: "createTime",
            type: "input",
            // search: true,
            minWidth:150,
            display:false,
            overHidden: true
          }
        ]
      },
      data: [],
      excelForm: {},
      excelOption: {
        submitBtn: false,
        emptyBtn: false,
        column: [
          {
            label: "模板上传",
            prop: "excelFile",
            type: "upload",
            drag: true,
            loadText: "模板上传中，请稍等",
            span: 24,
            propsHttp: {
              res: "data",
            },
            tip: "请上传 .xls,.xlsx 标准格式文件",
            action: "/api/rb-core/terminal/terminal/import",
          },
          {
            label: "模板下载",
            prop: "excelTemplate",
            formslot: true,
            span: 24,
          },
        ],
      },
      terminalDialog: {
        disabled: false,
        visible: false,
        type: 'add',
        text: '新建'
      },//表格右侧操作add,edit,view
      terminalDForm: {},//终端表单
      rptNoList: [],//类型
      rpsNoList: [],//供应商名称
      rpmNoList: [],//型号
      rpiNoList:[],//商户号
      storeNoList: [],//门店
      rpstNoList: [],//收银员
      hasPayPwd:false,
      twoPwdShow:false,
      towPwdForm:{
        towPwd:'',
        ids:'',
        rprNo:"",
        rsSn:"",
        rptNo:"",
        rpiNo:""
      }

    };
  },
  computed: {
    ...mapGetters(["permission","selectMer","userInfo"]),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.terminal_add, false),
        viewBtn: this.vaildData(this.permission.terminal_view, false),
        delBtn: this.vaildData(this.permission.terminal_delete, false),
        editBtn: this.vaildData(this.permission.terminal_edit, false)

      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids.join(",");
    },
  },
  watch: {
    // 'search.rptNo'(newVal, oldVal) {
    //     if (newVal !== oldVal) {
    //       this.search.rpsNo = '';
    //       const rpsNo = this.findObject(this.option.column, "rpsNo");
    //       rpsNo.dicData = [];
    //       this.search.rpmNo = '';
    //       const rpmNo = this.findObject(this.option.column, "rpmNo");
    //       rpmNo.dicData = [];
    //     }
    //   },
  },
  created() {
    this.initRequest()
  },
  methods: {
    /** @func 改变查询商户*/
    searchrpiNoChange(value){
      if (value) {
        this.search.storeNo = "";
        this.search.rpstNo = "";
        this.searchParams.rpiNo = value;

        let params = {
          rpiNo: value
        };
        this.$refs['searchRpsSelect'].getOptionList(params);
        this.$refs['searchRpstSelect'].getOptionList(params);
      }
    },
    /** @func 清除查询商户*/
    searchrpiNoClear(){
      this.search.storeNo = "";
      this.search.rpstNo = "";
      this.searchParams.rpiNo = "";

      this.$refs['searchRpsSelect'].getOptionList({rpiGroup:this.selectMer.rpiNo});
      this.$refs['searchRpstSelect'].getOptionList({rpiGroup:this.selectMer.rpiNo});
    },
    /** @func 改变查询门店*/
    searchrpsNoChange(value){
      if (value) {
        this.search.rpstNo = "";

        let params = {
          rpsNo: value,
        };
        if(this.selectMer.userMerchantType ===1){
          // params.rpiGroup=this.selectMer.rpiNo;
          if(this.search.rpiNo){
            params.rpiNo=this.search.rpiNo;
          }
        }else if(this.selectMer.userMerchantType ===2){
          params.rpiNo=this.selectMer.rpiNo
        }else{
          params.rpiNo=this.search.rpiNo
        }

        this.$refs['searchRpstSelect'].getOptionList(params);
      }
    },
    /** @func 清除门店*/
    searchrpsNoClear(){
      this.search.rpstNo = "";

      this.$refs['searchRpstSelect'].getOptionList();
    },
    // 二级密码 关闭回调
    handleCloseTwoPwd(done){
      this.towPwdForm.towPwd="";
      done()
    },
    // 二级密码 确定
    twoPwdSubmit(){
      let that = this;
      this.$refs.towPwdForm.validate((valid) => {
        if (valid) {
          if(that.towPwdForm.rptNo == 18){
            remove(that.towPwdForm.ids,md5(that.towPwdForm.towPwd+'')).then((res) => {
              that.onLoad(this.page);
              that.$message({
                type: "success",
                message: res.data.msg
              });
              that.$refs.crud.toggleSelection();
              that.twoPwdShow = false;
              that.towPwdForm.towPwd="";
            });
          } else if(that.towPwdForm.rptNo == 20){
             let param ={
                rsSn:that.towPwdForm.rprNo,
                rpiNo:that.towPwdForm.rpiNo,
                payPwd:md5(that.towPwdForm.towPwd+'')
              }
            removePOS(param).then((res) => {
              that.onLoad(this.page);
              that.$message({
                type: "success",
                message: res.data.msg
              });
              that.$refs.crud.toggleSelection();
              that.twoPwdShow = false;
              that.towPwdForm.towPwd="";
            });
          } else if(that.towPwdForm.rptNo == 23){
             let param ={
                rsSn:that.towPwdForm.rprNo,
                rpiNo:that.towPwdForm.rpiNo,
                payPwd:md5(that.towPwdForm.towPwd+'')
              }
            removeSM(param).then((res) => {
              that.onLoad(this.page);
              that.$message({
                type: "success",
                message: res.data.msg
              });
              that.$refs.crud.toggleSelection();
              that.twoPwdShow = false;
              that.towPwdForm.towPwd="";
            });
          } else { 
            let param ={
              payPwd:md5(that.towPwdForm.towPwd+''),
              rpiNo: that.towPwdForm.rpiNo,
              rprNo: that.towPwdForm.rprNo
            }
            
            terRemove(param).then((res) => {
              that.onLoad(this.page);
              that.$message({
                type: "success",
                message: res.data.msg
              });
              that.$refs.crud.toggleSelection();
              that.twoPwdShow = false;
              that.towPwdForm.towPwd="";
            });
          }
        } else {
          return false;
         }
      });
    },
    // 是否 有二级密码
    getUserHasInfo(){
      userHasInfo().then(res =>{
        this.hasPayPwd = res.data.data.hasPayPwd;
      }, error => {
        window.console.log(error);
      })
    },
    // 跳转用户信息页面
    toUserInfo(){
      this.$confirm('请您先设置二级密码，再操作！', '提示', {
        confirmButtonText: '确定',
        showCancelButton:false,
        type: 'warning'
      }).then(() => {
        this.$router.push({ path: '/info/index', query: { index: 1 }})
      })
    },
    //查看二维码
    getqrCodeUrl(item) {
      if (item.rpmNo == "R00001"||item.rptNo == '32') {
        this.rqUrlImage[0] = item.qrcodeUrl;
        this.payVoucherDialog = true;
      }
    },

    // 新建
    handleSave() {
      this.terminalDialog.visible = true;
      this.terminalDialog.type = 'add';
      this.terminalDialog.text = "新建终端";
      this.terminalDialog.disabled = false;
    },
    // 编辑
    handleEdit(row) {
      this.terminalDialog.visible = true;
      this.terminalDialog.type = 'edit';
      this.terminalDialog.text = "修改终端";
      this.terminalDialog.disabled = false;
      this.terminalDForm = JSON.parse(JSON.stringify(row));
    },
    handleView(row) {
      this.terminalDialog.visible = true;
      this.terminalDialog.type = 'view';
      this.terminalDialog.text = "查看";
      this.terminalDialog.disabled = true;
      // row.rtPara = JSON.stringify(row.rtPara).replace(/\\"/g, '"')
      this.terminalDForm = JSON.parse(JSON.stringify(row));

    },
    rowDel(row) {
      if(!this.hasPayPwd){
        this.toUserInfo()
      } else {
        this.towPwdForm.ids = row.id;
        this.towPwdForm.rptNo = row.rptNo;//设备类型编号
        this.towPwdForm.rpiNo = row.rpiNoCopy,//商户编号
        this.towPwdForm.rprNo = row.rsSn

        this.twoPwdShow = true;
      }
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      if(!this.hasPayPwd){
        this.toUserInfo();
      } else {
        this.towPwdForm.ids = this.ids;
        this.twoPwdShow = true;
      }
    },
    beforeOpen(done, type) {
      if (["edit", "view"].includes(type)) {
        getDetail(this.form.id).then((res) => {
          this.form = res.data.data;
        });
      }
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      let paramObj = {
        eqType: 1
      }
      if(this.selectMer.userMerchantType === 1){
        // 集团商户
        paramObj.rpiGroup=this.selectMer.rpiNo;
      }else if(this.selectMer.userMerchantType === 2){
        // 商户
        paramObj.rpiNo=this.selectMer.rpiNo;
        if(!this.query.rpstNo){
          paramObj.rpstNo=this.selectMer.rpstNo;
        }
      }else if(this.selectMer.userMerchantType === 3){
        // 门店
        paramObj.rpsNo = this.selectMer.rpsNo;
        paramObj.rpiNo=this.selectMer.rpiNo;
        if(!this.query.rpstNo){
          paramObj.rpstNo=this.selectMer.rpstNo;
        }
      }else if(this.selectMer.userMerchantType === 4){
        // 收银员
        paramObj.rpstNo = this.selectMer.rpstNo;
        paramObj.rpiNo=this.selectMer.rpiNo;
      }
      

      this.loading = true;
      getList(
        page.currentPage,
        page.pageSize,
        Object.assign(params, this.query, paramObj)
      ).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    },
    handleImport() {
      this.excelBox = true;
    },
    handleExport() {
      let exQuery = ``;
      exQuery += `&rtName=${this.search.rtName}`;
      exQuery += `&rtNo=${this.search.rtNo}`;
      exQuery += `&rpsNo=${this.search.rpsNo}`;
      exQuery += `&rptNo=${this.search.rptNo}`;
      exQuery += `&rpmNo=${this.search.rpmNo}`;
      exQuery += `&rsSn=${this.search.rsSn}`;
      // exQuery += `&rpiNo=${this.search.rpiNo}`;
      exQuery += `&storeNo=${this.search.storeNo}`;
      exQuery += `&rpstNo=${this.search.rpstNo}`;
      exQuery += `&rtPara=${this.search.rtPara}`;
      exQuery += `&isOn=${this.search.isOn}`;
      this.$confirm("是否导出用户数据?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        window.open(
          `/api/rb-merchant/merchant/terminal/terminal/export?${
            this.website.tokenHeader
          }=${getToken()}` + exQuery
        );
      });
    },
    handleTemplate() {
      window.open(
        `/api/rb-merchant/merchant/terminal/terminal/export-template?${
          this.website.tokenHeader
        }=${getToken()}`
      );
    },
    uploadAfter(res, done) {
      this.excelBox = false;
      this.refreshChange();
      done();
    },
    sortChange(val) {
      if (val.order == "ascending") {
        //正序
        this.query.ascs = val.prop;
        this.query.descs = "";
      } else {
        //倒序
        this.query.descs = val.prop;
        this.query.ascs = "";
      }
      this.refreshChange();
    },
    // 终端---新建弹框 关闭前回调
    terminalDialogHandleClose(done) {
      this.$refs['terminalDialog'].initEmpty();
      done();
    },
    // 复制
    clickCopy(ht) {
      const save = function(e) {
        e.clipboardData.setData('text/plain', ht)
        e.preventDefault() // 阻止默认行为
      }
      const once = {
        once: true
      }
      document.addEventListener('copy', save,once) // 添加一个copy事件
      document.execCommand('copy') // 执行copy方法
      this.$message({ message: '复制成功', type: 'success' })
    },
    // 终端---新建弹框提交
    terminalDialogSubmit() {
      /**
       * add  新建
       * edit 修改
       * view 查看详情
       */
      let that = this;
      if (this.terminalDialog.type !== 'view') {
        this.$refs['terminalDialog'].$refs['terminalDialogForm'].validate((valid) => {
          if (valid) {

            if(this.$refs['terminalDialog'].terminalDialogForm.rpmNo == 'A00001'){
              this.$refs['terminalDialog'].terminalDialogForm.rsSn = ''
            }

            let param = {
              ...this.$refs['terminalDialog'].terminalDialogForm
            }
            if(this.selectMer.userMerchantType === 1){
              // 集团商户
              param.rpiGroup=this.selectMer.rpiNo;
            }else{
              param.rpiNo=this.selectMer.rpiNo;
            }

            if (this.terminalDialog.type == 'add') {
              if(param.rpmNo == 'A00001'){
                addTerminal(param).then((res) => {
                  const h = this.$createElement;
                  this.$msgbox({
                    title: '',
                    message: h('div', null, [
                      h('h4', { style:'margin:0 0 10px 0;color:#f00;' }, res.data.msg),
                      h('div', null, `终端号：${res.data.data.appkey}`),
                      h('div', null, `密钥：${res.data.data.secret}`)
                    ]),
                    showCancelButton: true,
                    confirmButtonText: '确定',
                    cancelButtonText: '复制',
                    showClose:false,
                    beforeClose: (action,instance,done) => {
                      if (action === 'confirm') {
                        // instance.confirmButtonLoading = true;
                        // instance.confirmButtonText = '执行中...';
                        done();
                      } else {
                        let text = `终端号：${res.data.data.appkey},密钥：${res.data.data.secret}`;
                        that.clickCopy(text);
                      }
                    }
                  }).then(() => {
                    that.onLoad(this.page);
                    that.$refs['terminalDialog'].initEmpty();
                    that.$message({
                      type: "success",
                      message: "操作成功!",
                    });
                    that.terminalDialog.visible = false;
                  });
                },(error) => {
                    window.console.log(error);
                  }
                );
              }else{
                addTerminal(param).then(() => {
                  that.onLoad(this.page);
                  that.$refs['terminalDialog'].initEmpty();
                  that.$message({
                    type: "success",
                    message: "操作成功!",
                  });
                  that.terminalDialog.visible = false;
                },(error) => {
                    window.console.log(error);
                  }
                );
              }
            } else if (this.terminalDialog.type == 'edit') {
              editTerminal(param).then(() => {
                this.onLoad(this.page);
                this.$refs['terminalDialog'].initEmpty();

                this.$message({
                  type: "success",
                  message: "操作成功!",
                });
                this.terminalDialog.visible = false;
              },(error) => {
                  window.console.log(error);
                }
              );
            }
          }
        });

      }
    },
    // 获取 类型名称
    requestRptNo() {
      getRptNo(-1, -1,{eqType:1}).then((res) => {
        this.rptNoList = res.data.data.records;
        let column = this.findObject(this.option.column, "rptNo");
        column.dicData = this.rptNoList;
      })
    },
    // 获取型号
    apiModelListList(rptNo = "") {
      let param ={};
      if(rptNo){
        param.rptNo = rptNo;
      }
      getModelList(-1, -1, param).then((res) => {
        this.rpmNoList = res.data.data.records;
        let column = this.findObject(this.option.column, "rpmNo");
        column.dicData = this.rpmNoList;
        this.$forceUpdate();
      })
    },
    // 初始化
    initRequest() {
      this.getUserHasInfo();//是否 有二级密码

      this.isSelect();

      this.requestRptNo();//获取 类型

      this.apiModelListList();//型号
    },
    isSelect(){
      if(!this.selectMer.rpiNo){
        this.tipsShow=true;
      }else if(this.selectMer.userMerchantType === 1){
        /* userMerchantType----用户商户类型(0.无 1.集团商户 2.商户 3.门店 4.收银员) */
        // 集团商户
        this.searchParams.rpiGroup = this.selectMer.rpiNo;
        this.rpiGroup = this.selectMer.rpiNo;
        
        let rpiNoColumn = this.findObject(this.option.column, "rpiNo");
        let storeNoColumn = this.findObject(this.option.column, "storeNo");
        let rpstNoRolumn = this.findObject(this.option.column, "rpstNo");
        rpiNoColumn.search = true;
        storeNoColumn.search = true;
        rpstNoRolumn.search = true;
      } else if(this.selectMer.userMerchantType === 2){
        // 商户
        this.searchParams.rpiNo = this.selectMer.rpiNo;
        // this.rpiGroup = "";
        let storeNoColumn = this.findObject(this.option.column, "storeNo");
        let rpstNoRolumn = this.findObject(this.option.column, "rpstNo");
        storeNoColumn.search = true;
        rpstNoRolumn.search = true;
      } else if(this.selectMer.userMerchantType === 3) {
        // 门店
        this.searchParams={
          rpiNo:this.selectMer.rpiNo,
          rpsNo:this.selectMer.rpsNo
        }
        // this.rpiGroup = "";
        let rpstNoRolumn = this.findObject(this.option.column, "rpstNo");
        rpstNoRolumn.search = true;
        this.option.menu = false;
      }else{
        this.searchParams={
          rpiNo:this.selectMer.rpiNo,
          rpsNo:this.selectMer.rpsNo,
          rpstNo:this.selectMer.rpstNo
        }
        // this.rpiGroup = "";
        this.option.menu = false;
      }
    },
    // 获取商户
    // apiMerchantList(){
    //   getMerchantList(-1,-1,0).then((res) => {
    //     this.rpiNoList = res.data.data.records;
    //     const column = this.findObject(this.option.column, "rpiNo");
    //     column.dicData = this.rpiNoList;
    //   });
    // }
  }
};
</script>


<style lang="scss" scoped>
.rsSnblue{
  color: #409eff;
  cursor: pointer;
}
</style>